package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.dao.jdbc.ImportAndExportSchoolsTimeJdbc;
import com.wuseguang.springcloud.service.ImportAndExportSchoolsTimeService;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 学生进出校时长记录表 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-05-08
 */
@Service
public class ImportAndExportSchoolsTimeServiceImpl implements ImportAndExportSchoolsTimeService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ImportAndExportSchoolsTimeJdbc importAndExportSchoolsTimeJdbc;

    @Override
    public Result findInAndOutOfSchool(String deviceid) {

        Map<String, Object> stringObjectMap4 = importAndExportSchoolsTimeJdbc.queryDevice(deviceid);

        logger.info("查询班级所有学生进出校信息");
        //查询进出校信息
        String sql = "select  student_name,IFNULL(type,1) type , DATE_FORMAT(iaes.time,'%Y-%m-%d %T') time from student s " +
                " LEFT JOIN import_and_export_schools iaes on(s.id=iaes.student_id and iaes.id " +
                " in(select  max(iaes.id) from student s RIGHT JOIN import_and_export_schools iaes " +
                "on(s.id=iaes.student_id) where s.class_id = ?  GROUP BY s.id ) ) where s.class_id = ? ";

        List<Map<String,Object>> list = importAndExportSchoolsTimeJdbc.queryListMap(sql,
                stringObjectMap4.get("classid").toString(),
                stringObjectMap4.get("classid").toString());

        return new Result(list);
    }
}
